package guoanmaker.personal.business.service;

import java.util.Date;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.aliyun.oss.OSSClient;

import guoanmaker.personal.business.model.PersonalInvoice;
import guoanmaker.personal.business.model.PersonalUser;
import guoanmaker.personal.business.model.ReturnResult;
import guoanmaker.personal.business.repository.PersonalInvoiceRepository;
import guoanmaker.personal.business.repository.PersonalUserRepository;
import guoanmaker.personal.helper.common.abstractobj.ApiController;
import guoanmaker.personal.helper.common.util.OSSUnitHelper;

@Service
public class PersonalInvoiceService {
	@Resource
	private PersonalInvoiceRepository personalInvoiceRepository;
	
	@Resource
	private PersonalUserRepository personalUserRepository;

	/**
	 * 
	* @Description: 保存发票 
	* @param invoiceName 单位名称
	* @param taxNumber 纳税人识别码
	* @param invoiceType 发票类型  0为普通发票   1为专用发票
	* @param telephone 注册电话
	* @param address 注册地址
	* @param bankName 开户银行
	* @param bankNumber 银行账号
	* @param businessLicenseFile 营业执照副本图片上传地址
	* @param businessFile 营业执照图片上传地址 
	* @param isdefault 是否为默认发票,1为是,0为否
	* @param user_id 用户id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月16日 上午11:04:11     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult saveInvoice(String invoiceName,String taxNumber,String invoiceType,String telephone,String address,
			String bankName,String bankNumber,MultipartFile businessLicenseFile,MultipartFile businessFile,String isdefault,String user_id) {
		ReturnResult returnResult=new ReturnResult();
		PersonalUser user=personalUserRepository.findOne(user_id);
		if(user==null){
			returnResult.setKey("error");
			returnResult.setValue("用户不存在");
		}else{
			PersonalInvoice personalInvoice=new PersonalInvoice();
			personalInvoice.setUser(user);
			personalInvoice.setInvoiceName(invoiceName);
			personalInvoice.setTaxNumber(taxNumber);
			personalInvoice.setInvoiceType(invoiceType);
			personalInvoice.setTelephone(telephone);
			personalInvoice.setAddress(address);
			personalInvoice.setBankName(bankName);
			personalInvoice.setBankNumber(bankNumber);
			if(isdefault!=null&&isdefault.equals("1")){
				personalInvoiceRepository.updateInvoiceIsdefault(user_id);
				personalInvoice.setIsdefault(isdefault);
			}else{
				personalInvoice.setIsdefault("0");
			}
			personalInvoiceRepository.save(personalInvoice);
			if (businessLicenseFile != null && businessLicenseFile.getSize() > 0) {
				String fileName = businessLicenseFile.getOriginalFilename();
				String fileExtension = fileName.substring(fileName.lastIndexOf("."));
				OSSClient client = OSSUnitHelper.getOSSClient();
				OSSUnitHelper.uploadObjectOSS(client, businessLicenseFile,
						ApiController.INVOICE_PIC_URL + user_id + "/"+personalInvoice.getId()+"/", "businessLicense" + fileExtension);
				String url = OSSUnitHelper.getUrl(client, ApiController.INVOICE_PIC_URL + user_id + "/"+personalInvoice.getId()+"/",
						"businessLicense" + fileExtension);
				String urlstr = url.substring(0, url.indexOf("?")) + "?uploadTime=" + new Date().getTime();
				String uString=urlstr.replaceAll("http", "https");
				personalInvoice.setBusinessLicensePic(uString);
			}
			if (businessFile != null && businessFile.getSize() > 0) {
				String fileName = businessFile.getOriginalFilename();
				String fileExtension = fileName.substring(fileName.lastIndexOf("."));
				OSSClient client = OSSUnitHelper.getOSSClient();
				OSSUnitHelper.uploadObjectOSS(client, businessFile,
						ApiController.INVOICE_PIC_URL + user_id + "/"+personalInvoice.getId()+"/", "business" + fileExtension);
				String url = OSSUnitHelper.getUrl(client, ApiController.INVOICE_PIC_URL + user_id + "/"+personalInvoice.getId()+"/",
						"business" + fileExtension);
				String urlstr = url.substring(0, url.indexOf("?")) + "?uploadTime=" + new Date().getTime();
				String uString=urlstr.replaceAll("http", "https");
				personalInvoice.setBusinessPic(uString);
			}
			personalInvoiceRepository.save(personalInvoice);
			returnResult.setKey("success");
    		returnResult.setValue("保存发票成功");
		}
		return returnResult;
	}
	
	
	/**
	 * 
	* @Description: 保存发票 
	* @param invoiceName 单位名称
	* @param taxNumber 纳税人识别码
	* @param invoiceType 发票类型  0为普通发票   1为专用发票
	* @param telephone 注册电话
	* @param address 注册地址
	* @param bankName 开户银行
	* @param bankNumber 银行账号
	* @param businessLicenseFile 营业执照副本图片上传地址
	* @param businessFile 营业执照图片上传地址 
	* @param isdefault 是否为默认发票,1为是,0为否
	* @param user_id 用户id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月16日 上午11:04:11     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult saveInvoiceForPC(String invoiceName,String taxNumber,String invoiceType,String telephone,String address,
			String bankName,String bankNumber,String businessLicenseFile,String businessFile,String isdefault,String user_id) {
		ReturnResult returnResult=new ReturnResult();
		PersonalUser user=personalUserRepository.findOne(user_id);
		if(user==null){
			returnResult.setKey("error");
			returnResult.setValue("用户不存在");
		}else{
			PersonalInvoice personalInvoice=new PersonalInvoice();
			personalInvoice.setUser(user);
			personalInvoice.setInvoiceName(invoiceName);
			personalInvoice.setTaxNumber(taxNumber);
			personalInvoice.setInvoiceType(invoiceType);
			personalInvoice.setTelephone(telephone);
			personalInvoice.setAddress(address);
			personalInvoice.setBankName(bankName);
			personalInvoice.setBankNumber(bankNumber);
			if(isdefault!=null&&isdefault.equals("1")){
				personalInvoiceRepository.updateInvoiceIsdefault(user_id);
				personalInvoice.setIsdefault(isdefault);
			}else{
				personalInvoice.setIsdefault("0");
			}
			String uString="";
			String uString2="";
			if(businessLicenseFile!=null){
				if(!businessLicenseFile.startsWith("https")){
					uString=businessLicenseFile.replaceAll("http", "https");
				}else{
					uString=businessLicenseFile;
				}
				
			}
			personalInvoice.setBusinessLicensePic(uString);
			if(businessFile!=null){
				if(!businessFile.startsWith("https")){
					uString2=businessFile.replaceAll("http", "https");
				}else{
					uString2=businessFile;
				}
				
			}
			personalInvoice.setBusinessPic(uString2);
			personalInvoiceRepository.save(personalInvoice);
			returnResult.setKey("success");
    		returnResult.setValue("保存发票成功");
		}
		return returnResult;
	}
	
	/**
	 * 
	* @Description: 删除发票
	* @param id 发票id
	* @author Yvonne_MGL  
	* @date 2017年7月6日 下午3:57:42     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult deleteInvoice(String id){
		ReturnResult returnResult=new ReturnResult();
		personalInvoiceRepository.delete(id);
		returnResult.setKey("success");
		returnResult.setValue("删除发票成功");
		return returnResult;
	}
	
	/**
	 * 
	* @Description: 保存发票 
	* @param invoiceName 单位名称
	* @param taxNumber 纳税人识别码
	* @param invoiceType 发票类型  0为普通发票   1为专用发票
	* @param telephone 注册电话
	* @param address 注册地址
	* @param bankName 开户银行
	* @param bankNumber 银行账号
	* @param businessLicenseFile 营业执照副本图片上传地址
	* @param businessFile 营业执照图片上传地址 
	* @param isdefault 是否为默认发票,1为是,0为否
	* @param id 发票id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月16日 下午2:02:23     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult updateInvoice(String invoiceName,String taxNumber,String invoiceType,String telephone,String address,
			String bankName,String bankNumber,MultipartFile businessLicenseFile,MultipartFile businessFile,String isdefault,String id) {
		ReturnResult returnResult=new ReturnResult();
		PersonalInvoice personalInvoice=personalInvoiceRepository.findOne(id);
		if(personalInvoice==null){
			returnResult.setKey("error");
			returnResult.setValue("发票不存在");
		}else{
			if(invoiceName!=null&&!invoiceName.equals("")){
				personalInvoice.setInvoiceName(invoiceName);
			}
			
			if(taxNumber!=null&&!taxNumber.equals("")){
				personalInvoice.setTaxNumber(taxNumber);
			}
			
			if(invoiceType!=null&&!invoiceType.equals("")){
				personalInvoice.setInvoiceType(invoiceType);
			}
			
			if(telephone!=null&&!telephone.equals("")){
				personalInvoice.setTelephone(telephone);
			}
			
			if(address!=null&&!address.equals("")){
				personalInvoice.setAddress(address);
			}
			
			if(bankName!=null&&!bankName.equals("")){
				personalInvoice.setBankName(bankName);
			}
			
			if(bankNumber!=null&&!invoiceName.equals("")){
				personalInvoice.setBankNumber(bankNumber);
			}
			
			if(isdefault!=null&&!isdefault.equals("")){
				if(isdefault.equals("1")&&(personalInvoice.getIsdefault()==null||!personalInvoice.getIsdefault().equals("1"))){
					personalInvoiceRepository.updateInvoiceIsdefault(personalInvoice.getUser().getId());
				}
				personalInvoice.setIsdefault(isdefault);
			}
			if (businessLicenseFile != null && businessLicenseFile.getSize() > 0) {
				String fileName = businessLicenseFile.getOriginalFilename();
				String fileExtension = fileName.substring(fileName.lastIndexOf("."));
				OSSClient client = OSSUnitHelper.getOSSClient();
				OSSUnitHelper.uploadObjectOSS(client, businessLicenseFile,
						ApiController.INVOICE_PIC_URL + personalInvoice.getUser().getId() + "/"+personalInvoice.getId()+"/", "businessLicense" + fileExtension);
				String url = OSSUnitHelper.getUrl(client, ApiController.INVOICE_PIC_URL + personalInvoice.getUser().getId() + "/"+personalInvoice.getId()+"/",
						"businessLicense" + fileExtension);
				String urlstr = url.substring(0, url.indexOf("?")) + "?uploadTime=" + new Date().getTime();
				String uString=urlstr.replaceAll("http", "https");
				personalInvoice.setBusinessLicensePic(uString);
			}
			if (businessFile != null && businessFile.getSize() > 0) {
				String fileName = businessFile.getOriginalFilename();
				String fileExtension = fileName.substring(fileName.lastIndexOf("."));
				OSSClient client = OSSUnitHelper.getOSSClient();
				OSSUnitHelper.uploadObjectOSS(client, businessFile,
						ApiController.INVOICE_PIC_URL + personalInvoice.getUser().getId() + "/"+personalInvoice.getId()+"/", "business" + fileExtension);
				String url = OSSUnitHelper.getUrl(client, ApiController.INVOICE_PIC_URL + personalInvoice.getUser().getId() + "/"+personalInvoice.getId()+"/",
						"business" + fileExtension);
				String urlstr = url.substring(0, url.indexOf("?")) + "?uploadTime=" + new Date().getTime();
				String uString=urlstr.replaceAll("http", "https");
				personalInvoice.setBusinessPic(uString);
			}
			personalInvoiceRepository.save(personalInvoice);
			returnResult.setKey("success");
    		returnResult.setValue("保存发票成功");
		}
		return returnResult;
	}
	
	/**
	 * 
	* @Description: 保存发票 
	* @param invoiceName 单位名称
	* @param taxNumber 纳税人识别码
	* @param invoiceType 发票类型  0为普通发票   1为专用发票
	* @param telephone 注册电话
	* @param address 注册地址
	* @param bankName 开户银行
	* @param bankNumber 银行账号
	* @param businessLicenseFile 营业执照副本图片上传地址
	* @param businessFile 营业执照图片上传地址 
	* @param isdefault 是否为默认发票,1为是,0为否
	* @param id 发票id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月16日 下午2:02:23     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult updateInvoiceForPC(String invoiceName,String taxNumber,String invoiceType,String telephone,String address,
			String bankName,String bankNumber,String businessLicenseFile,String businessFile,String isdefault,String id) {
		ReturnResult returnResult=new ReturnResult();
		PersonalInvoice personalInvoice=personalInvoiceRepository.findOne(id);
		if(personalInvoice==null){
			returnResult.setKey("error");
			returnResult.setValue("发票不存在");
		}else{
			if(invoiceName!=null&&!invoiceName.equals("")){
				personalInvoice.setInvoiceName(invoiceName);
			}
			
			if(taxNumber!=null&&!taxNumber.equals("")){
				personalInvoice.setTaxNumber(taxNumber);
			}
			
			if(invoiceType!=null&&!invoiceType.equals("")){
				personalInvoice.setInvoiceType(invoiceType);
			}
			
			if(telephone!=null&&!telephone.equals("")){
				personalInvoice.setTelephone(telephone);
			}
			
			if(address!=null&&!address.equals("")){
				personalInvoice.setAddress(address);
			}
			
			if(bankName!=null&&!bankName.equals("")){
				personalInvoice.setBankName(bankName);
			}
			
			if(bankNumber!=null&&!invoiceName.equals("")){
				personalInvoice.setBankNumber(bankNumber);
			}
			
			if(isdefault!=null&&!isdefault.equals("")){
				if(isdefault.equals("1")&&(personalInvoice.getIsdefault()==null||!personalInvoice.getIsdefault().equals("1"))){
					personalInvoiceRepository.updateInvoiceIsdefault(personalInvoice.getUser().getId());
				}
				personalInvoice.setIsdefault(isdefault);
			}
			
			if(businessLicenseFile!=null&&!businessLicenseFile.equals("")){
				if(!businessLicenseFile.startsWith("https")){
					String uString=businessLicenseFile.replaceAll("http", "https");
					personalInvoice.setBusinessLicensePic(uString);
				}else{
					personalInvoice.setBusinessLicensePic(businessLicenseFile);
				}
				
			}
			if(businessFile!=null&&!businessFile.equals("")){
				if(!businessFile.startsWith("https")){
					String uString=businessFile.replaceAll("http", "https");
					personalInvoice.setBusinessPic(uString);
				}else{
					personalInvoice.setBusinessPic(businessFile);
				}
				
			}
			personalInvoiceRepository.save(personalInvoice);
			returnResult.setKey("success");
    		returnResult.setValue("保存发票成功");
		}
		return returnResult;
	}

}
